"use strict";
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
const common_vendor = require("../common/vendor.js");
const stores_userInfo = require("../stores/userInfo.js");
const api_index = require("../api/index.js");
const userInfoStore = stores_userInfo.useUserInfoStore();
class Request {
  constructor(config) {
    __publicField(this, "baseUrl");
    __publicField(this, "timeout");
    __publicField(this, "header");
    this.baseUrl = config.baseUrl;
    this.timeout = config.timeout || 8e3;
    this.header = config.header || {};
  }
  _request(options) {
    const whiteUrls = [
      "/wx-login/getWxOpenId",
      "/wx-login/wxLogin",
      "/anti-fraud/listPage",
      //反诈宣传列表
      "/anti-fraud/selectOne",
      //反诈宣传详情
      "/firefighting/listPage",
      //消防安全列表
      "/firefighting/selectOne",
      //消防安全详情
      "/preventive/listPage",
      //防范提示列表
      "/preventive/selectOne",
      //防范提示详情
      "/community/selectAll",
      "/loginTest",
      "/wx-login/getCommunityByPoint",
      "/affairs-guide/listPage",
      //办事指南首页列表
      "/affairs-guide/selectOne",
      //办事指南详情
      "/affairs-guide/clickHandle",
      //办事指南浏览量
      "/affairs-guide/listGroup",
      //办事指南列表
      "/affairs-guide/search"
      //办事指南搜索
    ];
    common_vendor.index.addInterceptor("request", {
      invoke(args) {
        if (!whiteUrls.find((item) => item == options.url)) {
          args.header = {
            // 请求头带token
            Authorization: "Bearer " + userInfoStore.token
          };
        }
      },
      success(res) {
      },
      fail(err) {
        console.log(err);
      }
    });
    return new Promise((resolve, reject) => {
      common_vendor.index.request({
        url: this.baseUrl + options.url,
        timeout: this.timeout,
        header: {
          ...this.header,
          ...options.headers
          // 可以请求时传自定义请求头
        },
        method: options.method || "GET",
        data: options.data,
        success: (res) => {
          const data = res.data;
          if (data.code == 200) {
            resolve(data);
          } else if (data.code === 401) {
            api_index.getToken({
              userId: userInfoStore.userInfo.userId,
              userType: userInfoStore.userInfo.userType
            }).then((res2) => {
              const { data: data2 } = res2;
              userInfoStore.setToken(data2);
              this._request(options);
            }).catch((err) => {
              common_vendor.index.$u.toast(err);
            });
          } else {
            reject(data.msg);
          }
        },
        fail: (err) => {
          common_vendor.index.$u.toast(this.baseUrl + options.url);
          common_vendor.index.showToast({
            title: "请求失败",
            icon: "error"
          });
        },
        complete: () => {
        }
      });
    });
  }
  get(options) {
    return this._request({
      ...options
    });
  }
  post(options) {
    return this._request({
      ...options,
      method: "POST"
    });
  }
  put(options) {
    return this._request({
      ...options,
      method: "PUT"
    });
  }
  del(options) {
    return this._request({
      ...options,
      method: "DELETE"
    });
  }
  // 上传文件
  upload(options) {
    common_vendor.index.showLoading({
      title: "上传中"
    });
    return new Promise((resolve, reject) => {
      common_vendor.index.uploadFile({
        url: this.baseUrl + options.url,
        header: {
          // 请求头带token
          "Authori-zation": "Bearer " + userInfoStore.token,
          ...options.headers
        },
        filePath: options.data.filePath,
        name: options.data.name,
        formData: {
          filename: options.data.name
        },
        success: (res) => {
          const data = res.data;
          if (data.code == 200) {
            resolve(data);
          } else if (data.code === 401) {
            common_vendor.index.$u.toast("登录状态已过期，请重新登录");
          } else {
            reject(data.msg);
          }
        },
        fail: (err) => {
          common_vendor.index.hideLoading();
          common_vendor.index.showToast({
            title: "请求失败",
            icon: "error"
          });
        }
      });
    });
  }
}
exports.Request = Request;
